package com.zx.template.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;

/**
 * @author szh
 * @date 2022/8/3 14:00
 */
public class PageUtils {

    public static <T> PageBean<T> pageToPageBean(IPage<T> page) {
        return PageBean.<T>builder()
                .content(page.getRecords())
                .size((int) page.getSize())
                .total((int) page.getTotal())
                .current((int) page.getCurrent())
                .build();
    }

    public static <T> Page<T> pageBeanToPage(PageBean<T> page) {
        return page == null || page.getCurrent() == null ?
                new Page<>(0L, 20L) :
                new Page<>(page.getCurrent(), page.getSize());
    }

    public static <T> PageBean<T> emptyPageBean(Long current, Long size) {
        return PageBean.<T>builder()
                .content(new ArrayList<>())
                .size(size.intValue())
                .total(0)
                .current(current.intValue())
                .build();
    }

}

